//
//  BDRuzhuCityChooseView.m
//  BadBoy
//
//  Created by wen on 2017/4/18.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDRuzhuCityChooseView.h"
#import "BDCityInfo.h"

@interface BDRuzhuCityChooseView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *bkView;

@property (nonatomic, strong) NSMutableArray *datasource;
@property (nonatomic, strong) NSMutableArray *resultArray;

@property (nonatomic, copy) void(^finished)(NSArray *array);
@end

@implementation BDRuzhuCityChooseView

+ (void)showWithFinished:(void(^)(NSArray *array))finished{
    BDRuzhuCityChooseView *chooseView = [[BDRuzhuCityChooseView alloc] init];
    chooseView.contentView.top = kBDScreenHeight;
    chooseView.finished = finished;
    [[UIApplication sharedApplication].keyWindow addSubview:chooseView];
    
    [UIView animateWithDuration:0.3 animations:^{
        chooseView.contentView.bottom = kBDScreenHeight;
        chooseView.bkView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    } completion:^(BOOL finished) {
        
    }];
}

- (NSMutableArray *)datasource{
    if (!_datasource) {
        self.datasource = [NSMutableArray array];
    }
    return _datasource;
}

- (NSMutableArray *)resultArray{
    if (!_resultArray) {
        self.resultArray = [NSMutableArray array];
    }
    return _resultArray;
}

- (instancetype)init{
    CGRect frame = CGRectMake(0, 0, kBDScreenWidth, kBDScreenHeight);
    if (self = [super initWithFrame:frame]) {
        [self requestDatasource];
        [self.tableView reloadData];
    }
    return self;
}

- (void)requestDatasource{
    self.datasource = [BDCityInfo getCityArrayWithType:BDCityTypeProvice].mutableCopy;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cityCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cityCell"];
    }
    BDCityInfo *cityInfo = self.datasource[indexPath.row];
    cell.textLabel.text = cityInfo.name;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BDCityInfo *cityInfo = self.datasource[indexPath.row];
    [self.resultArray addObject:cityInfo];
    self.datasource = [BDCityInfo getCityArrayWithParentId:cityInfo.infoID].mutableCopy;

    if (self.datasource.count == 0) {
        if (self.finished) {
            self.finished(self.resultArray);
        }
        [self hidden];
    }else{
        [self.tableView reloadData];
        [self.tableView setContentOffset:CGPointMake(0, 0)];
    }
}

- (void)hidden{
    [UIView animateWithDuration:0.3 animations:^{
        self.contentView.top = kBDScreenHeight;
        self.bkView.backgroundColor = [UIColor clearColor];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)tapGR{
    [self hidden];
}

- (UIView *)bkView{
    if (!_bkView) {
        self.bkView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, kBDScreenHeight)];
        _bkView.backgroundColor = [UIColor clearColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR)];
        [_bkView addGestureRecognizer:tap];
        [self insertSubview:_bkView atIndex:0];
    }
    return _bkView;
}

- (UIView *)contentView{
    if (!_contentView) {
        self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, kBDScreenHeight / 2.0)];
        _contentView.backgroundColor = [UIColor whiteColor];
        UILabel *label = [[UILabel alloc] init];
        label.text = ASLocalizedString(@"选择城市");
        label.font = [UIFont systemFontOfSize:16];
        label.textColor = [UIColor colorWithHexString:@"#1c1c1c"];
        [label sizeToFit];
        label.centerX = kBDScreenWidth / 2.0;
        label.height = 45;
        [_contentView addSubview:label];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 0.5)];
        line.backgroundColor = kBDColorGray;
        line.top = label.bottom;
        [_contentView addSubview:line];
        
        [_contentView addSubview:self.tableView];
        self.tableView.height = _contentView.height - line.bottom;
        self.tableView.bottom = _contentView.height;
        [self addSubview:_contentView];
    }
    return _contentView;
}

- (UITableView *)tableView{
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, kBDScreenHeight / 3.0) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

@end
